package com.chagee.store.operation.server.service.third;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.chagee.store.operation.api.bo.AppealExtraVO;
import com.chagee.store.operation.api.bo.OrderExtJsonBO;
import com.chagee.store.operation.api.enums.*;
import com.chagee.store.operation.common.constant.CommonConstant;
import com.chagee.store.operation.common.enums.LarkMessageTypeEnum;
import com.chagee.store.operation.server.entity.*;
import com.chagee.store.operation.server.model.info.LarkMessageInfo;
import com.chagee.store.operation.server.service.biz.FeiShuMessageBizService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;

/**
 * @Author : hk
 * @Date : 2024/10/30
 */

@Slf4j
@Service
@RequiredArgsConstructor
public class FeiShuSendService {

	@Resource
	private FeiShuMessageBizService feiShuMessageBizService;

	private final SimpleDateFormat format = new SimpleDateFormat(CommonConstant.FORMAT);

	/**
	 * 任务审批通过时发送消息通知
	 */
	@Async
	public void sendTaskApprovalFeiShuMessage(WorkOrderDO workOrderDO, TaskExecutionDO taskExecutionDO) {
		log.info("工单{},{}任务审核结果{}", workOrderDO.getWorkOrderNumber(), taskExecutionDO.getTaskName(), AppealStatusEnum.getByType(taskExecutionDO.getAppealStatus()).getDesc());
		//发送消息
		LarkMessageInfo info = new LarkMessageInfo();
		info.setStoreNo(workOrderDO.getStoreCode());
		info.setWorkOrderNumber(taskExecutionDO.getWorkOrderNumber());
		info.setOrderCreateTime(format.format(workOrderDO.getCreateTime()));
		info.setTaskName(taskExecutionDO.getTaskName());
		if (AppealStatusEnum.PASSED.name().equals(AppealStatusEnum.getByType(taskExecutionDO.getAppealStatus()).name())) {
			info.setType(LarkMessageTypeEnum.appealSuccessNotice.getType());
		} else {
			info.setType(LarkMessageTypeEnum.taskAppealRejected.getType());
		}

		if (StringUtils.isNotBlank(taskExecutionDO.getExtra())) {
			AppealExtraVO appealExtraVO = JSON.parseObject(taskExecutionDO.getExtra(), AppealExtraVO.class);
			info.setHandleAppealReason(appealExtraVO.getHandleAppealReason());
		}
		try {
			feiShuMessageBizService.sendMessage(info);
		} catch (Exception e) {
			log.error("打烊工单{} 申述任务发送飞书通知失败:", taskExecutionDO.getWorkOrderNumber(), e);
		}
	}

	@Async
	public void orderAppealFeiShuMsg(WorkOrderDO workOrderDO) {
		LarkMessageInfo info = new LarkMessageInfo();
		info.setStoreNo(workOrderDO.getStoreCode());
		info.setWorkOrderNumber(workOrderDO.getWorkOrderNumber());
		if (StringUtils.equals(OrderTaskAcceptanceEnum.NO_PASS.getType(), workOrderDO.getContinuousAcceptance())) {
			info.setType(LarkMessageTypeEnum.order_appeal.getType());
		} else {
			info.setType(LarkMessageTypeEnum.expired_appeal.getType());
		}
		try {
			feiShuMessageBizService.sendMessage(info);
		} catch (Exception e) {
			log.error("过期工单{}申述发送飞书通知失败:", workOrderDO.getWorkOrderNumber(), e);
		}
	}

	@Async
	public void sendOrderHandleFeiShuMsg(WorkOrderDO workOrderDO) {
		LarkMessageInfo info = new LarkMessageInfo();
		info.setStoreNo(workOrderDO.getStoreCode());
		info.setWorkOrderNumber(workOrderDO.getWorkOrderNumber());

		//任务连续三天不合格类型工单审核
		if (StringUtils.isNotBlank(workOrderDO.getContinuousAcceptance())) {
			info.setType(LarkMessageTypeEnum.handle_order_appeal.getType());
			if (OrderAppealStatusEnum.N.getType().equals(workOrderDO.getAppealResult())) {
				info.setType(LarkMessageTypeEnum.specialOrderAppealRejected.getType());
			}
		} else {
			info.setType(LarkMessageTypeEnum.handle_appeal.getType());
			if (OrderAppealStatusEnum.N.getType().equals(workOrderDO.getAppealResult())) {
				info.setType(LarkMessageTypeEnum.orderAppealRejected.getType());
			}
		}

		info.setOrderCreateTime(format.format(workOrderDO.getCreateTime()));
		info.setAppealResult(workOrderDO.getAppealResult());
		if (OrderAppealStatusEnum.Y.getType().equals(workOrderDO.getAppealResult())) {
			info.setOrderAcceptanceStatus(Integer.parseInt(workOrderDO.getDocumentScore()) < 80 ? OrderAcceptanceEnum.NO.getDesc() : OrderAcceptanceEnum.YES.getDesc());
		}
		if (StringUtils.isNotBlank(workOrderDO.getExtra())) {
			OrderExtJsonBO extJsonBO = JSON.parseObject(workOrderDO.getExtra(), new TypeReference<OrderExtJsonBO>() {
			});
			info.setHandleAppealReason(extJsonBO.getHandleAppealReason());
		}
		try {
			feiShuMessageBizService.sendMessage(info);
		} catch (Exception e) {
			log.error("处理过期工单申述{}发送飞书通知失败:", workOrderDO.getWorkOrderNumber(), e);
		}
	}

	/**
	 * 门店结算驳回
	 * @param storeSettlementDO
	 */
	@Async
	public void storeRejectedMsg(StoreSettlementDO storeSettlementDO) {
		LarkMessageInfo info = new LarkMessageInfo();
		info.setStoreNo(storeSettlementDO.getStoreNo());

		info.setType(LarkMessageTypeEnum.storeRejected.getType());
		info.setSettlementNo(storeSettlementDO.getSettlementNo());
		info.setSupplierName(storeSettlementDO.getSupplier());
		try {
			feiShuMessageBizService.sendMessage(info);
		} catch (Exception e) {
			log.error("门店结算驳回通知{}发送飞书通知失败:", storeSettlementDO.getSettlementNo(), e);
		}
	}

	@Async
	public void supplierRejectedMsg(StoreSettlementDO storeSettlementDO) {
		LarkMessageInfo info = new LarkMessageInfo();
		info.setStoreNo(storeSettlementDO.getStoreNo());

		info.setType(LarkMessageTypeEnum.supplierRejected.getType());
		info.setSettlementNo(storeSettlementDO.getSettlementNo());
		info.setSupplierName(storeSettlementDO.getSupplier());
		try {
			feiShuMessageBizService.sendMessage(info);
		} catch (Exception e) {
			log.error("供应商结算驳回通知{}发送飞书通知失败:", storeSettlementDO.getSettlementNo(), e);
		}
	}

	@Async
	public void sendFeiShuMsg(LarkMessageInfo messageInfo) {
		try {
			feiShuMessageBizService.sendMessage(messageInfo);
		} catch (Exception e) {
			log.error("供应商上传发票通知发送飞书通知失败:", e);
		}
	}
}
